package com.huilian.iotbox.admin.wap.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.huilian.iotbox.admin.wap.dao.CommodityGroupDao;
import com.huilian.iotbox.admin.wap.service.CommodityGroupService;
import com.huilian.iotbox.data.common.ServerResponse;
import com.huilian.iotbox.data.dto.CommodityGroupDto;
import com.huilian.iotbox.data.enums.CommodityGroupTypeEnum;
import com.huilian.iotbox.data.enums.CommodityTypeEnum;
import com.huilian.iotbox.data.po.CommodityGroup;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

@Service
public class CommodityGroupServiceImpl implements CommodityGroupService {

    @Resource
    private CommodityGroupDao commodityGroupDao;

    @Override
    public PageInfo<CommodityGroup> findByUserIdAndType(CommodityGroupDto groupDto) {
        // 判断type是否是空的，如果是空的就默认查询类型2
        if(StringUtils.isEmpty(groupDto.getType()) || CommodityTypeEnum.PACKAGE.getCode().equals(groupDto.getType()) || !StringUtils.isEmpty(groupDto.getBranchType())){
            groupDto.setType(CommodityTypeEnum.CURRENT.getCode());
        }
        PageHelper.orderBy("createTime DESC");
        PageHelper.startPage(groupDto.getPageNum(), groupDto.getPageSize(), true);
        List<CommodityGroup> list = commodityGroupDao.findByUserIdAndType(groupDto);
        return new PageInfo<>(list);
    }

    @Override
    public Integer insertOrUpdate(CommodityGroup commodityGroup) {

        if(!StringUtils.isEmpty(commodityGroup.getBranchType())){
            commodityGroup.setType(CommodityGroupTypeEnum.CURRENT.getCode());
        }
        if (!StringUtils.isEmpty(commodityGroup.getIsDefault()) && commodityGroup.getIsDefault()) {
            CommodityGroupDto commodityGroupDto = new CommodityGroupDto();
            commodityGroupDto.setIsDefault(true);
            commodityGroupDto.setUserId(commodityGroup.getUserId());
            CommodityGroup defaultCommodityGroup = commodityGroupDao.findOneCommodityGroupByCondition(commodityGroupDto);

            if (!StringUtils.isEmpty(defaultCommodityGroup)) {
                CommodityGroup updateCommodityGroup = new CommodityGroup();
                updateCommodityGroup.setId(defaultCommodityGroup.getId());
                updateCommodityGroup.setIsDefault(false);
                updateCommodityGroup.setUserId(commodityGroup.getUserId());
                commodityGroupDao.update(updateCommodityGroup);
            }
        }

        if (!StringUtils.isEmpty(commodityGroup.getId())) {
            return commodityGroupDao.update(commodityGroup);
        }


        return commodityGroupDao.insert(commodityGroup);
    }

    @Override
    public ServerResponse deleteById(CommodityGroupDto commodityGroupDto) {
        if (commodityGroupDao.getGroupedProductCount(commodityGroupDto) > 0) {
            return ServerResponse.createByErrorMessage("该分组下存在商品无法删除!");
        }
        return ServerResponse.createBySuccess(commodityGroupDao.deleteById(commodityGroupDto));
    }

    @Override
    public int insert(CommodityGroup newCommodityGroup) {
        return commodityGroupDao.insert(newCommodityGroup);
    }

    @Override
    public CommodityGroup findOneCommodityGroupByCondition(CommodityGroupDto commodityGroupDto) {
        return commodityGroupDao.findOneCommodityGroupByCondition(commodityGroupDto);
    }

    /**
     * 查询当前类型的默认分组
     *
     * @param commodityGroup
     * @return
     */
    @Override
    public ServerResponse getDefaultCommodityGroup(CommodityGroupDto commodityGroup) {
        CommodityGroupDto commodityGroupDto = new CommodityGroupDto();
        commodityGroupDto.setIsDefault(true);
        commodityGroupDto.setUserId(commodityGroup.getUserId());
        commodityGroupDto.setType(commodityGroup.getType());
        CommodityGroup defaultCommodityGroup = commodityGroupDao.findOneCommodityGroupByCondition(commodityGroupDto);

        if (!StringUtils.isEmpty(defaultCommodityGroup)) {
            defaultCommodityGroup.setUserId(null);
        }

        return ServerResponse.createBySuccess(defaultCommodityGroup);
    }

    @Override
    public CommodityGroup getOneCommodityGroupByUserIdAndType(CommodityGroupDto groupDto) {
        return commodityGroupDao.getOneCommodityGroupByUserIdAndType(groupDto);
    }

}
